/*自动下拉补全 zhk */
var wordText=$("#word").val();
var autoNode=$("#auto");
var highlightindex=-1;//当前高亮的节点
$(function(){
    var wordInput=$("#word");
    var wordInputOffset=wordInput.offset();
    $("#auto").hide().css("border","1px black solid").css("position","absolute")
        .css("top",wordInputOffset.top+wordInput.height()+5+"px")
        .css("left",wordInputOffset.left-35+"px").width(wordInput.width()+2)
        .css("background-color","lightblue");
    wordInput.keyup(function (event){
        var myEvent=event||window.event;
        var keyCode=myEvent.keyCode;
        if(keyCode>=65&&keyCode<=90||keyCode==8||keyCode==46||keyCode==32){
            wordText=$("#word").val();
            autoNode=$("#auto");
            if(wordText!=""){
                $("#located").css("display","none");

                Do_get_link();

                setTimeout(Do_sleep,10)
                /* for (var i=1;i<=10;i++){
                     console.log(wordText)
                     $("#span"+i).html(wordText)
                 }*/


            }else{
                autoNode.hide();
                highlightindex=-1;
            }
        }else if(keyCode==38||keyCode==40){
            if(keyCode==38){//向上
                var autoNodes=$("#auto").children("div");
                if(highlightindex!=-1){
                    autoNodes.eq(highlightindex).css("background-color","lightblue");
                    highlightindex--;
                }else{
                    highlightindex=autoNodes.length-1;
                }
                if(highlightindex==-1){
                    highlightindex=autoNodes.length-1;
                }
                autoNodes.eq(highlightindex).css("background-color","deepskyblue");
            }
            if(keyCode==40){
                var autoNodes=$("#auto").children("div");
                if(highlightindex!=-1){
                    autoNodes.eq(highlightindex).css("background-color","lightblue");
                }
                highlightindex++;
                if(highlightindex==autoNodes.length){
                    highlightindex=0;
                }
                autoNodes.eq(highlightindex).css("background-color","deepskyblue");
            }
        }else if(keyCode==13){
            if(highlightindex!=-1){
                var comText=$("#auto").hide().children("div").eq(highlightindex).text();
                highlightindex=-1;
                $("#word").val(comText);
            }else{
                Dosearch();
                $("#auto").hide();
                $("#word").get(0).blur();//失去焦点
            }
        }
    })
    $("#button").click(function(){
        Dosearch();
    });

})

Do_get_link = function () {
    $.ajax({
        url:"search_text",
        data:{
            "search":wordText,
        },
        dataType:"json",
        type:"get",
        success:function (data) {
            var num = 0;
            $("#pp").empty();
            if (data.relation.length>=10){
                num = 10;
            }else {
                num = data.relation.length;
            }
            for (var i=1;i<=num;i++){
                $("#pp").append("<span class='sspan' id='span"+i+"'></span>")
                console.log(data.relation[i-1])
                var text = "";
                if (data.relation[i-1].length>31){
                    text = data.relation[i-1].slice(0,31)
                }else {
                    text = data.relation[i-1]
                }
                $("#span"+i).html(text)
            }
        }
    })
}

Do_sleep = function () {
    var wordNodes=$(".sspan");
    autoNode.html("");
    wordNodes.each(function(i){
        var wordNode=$(this);
        var newDivNode=$("<div>").attr("id",i);
        newDivNode.html(wordNode.text()).appendTo(autoNode);
        newDivNode.mouseover(function(){//鼠标进入
            if(highlightindex!=-1){
                $("#auto").children("div").eq(highlightindex)
                    .css("background-color","lightblue");
            }
            highlightindex=$(this).attr("id");
            $(this).css("background-color","deepskyblue");
        })
        newDivNode.mouseout(function(){//鼠标移除
            $(this).css("background-color","lightblue");
        })
        newDivNode.click(function(){//点击
            var comText=$(this).text();
            $("#auto").hide();
            highlightindex=-1;
            $("#word").val(comText);
        })
    })
    if(wordNodes.length>0){
        autoNode.show();
    }else{
        autoNode.hide();
        highlightindex=-1;
    }
}